<%--
  Created by IntelliJ IDEA.
  User: teacher
  Date: 2023/7/17
  Time: 9:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Ajax文件上传</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {
            $("#upload").click(function () {
                let formData = new FormData($("#uploadForm")[0]);
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/upload",
                    data: formData,
                    // 如果是表单数据,则使用默认值true
                    // 如果不是表单数据,则设置为false即可
                    processData: false,
                    // 如果不是表单数据,可以使用false代替即可
                    // 默认为application/x-www-form-urlencoded
                    contentType: false,
                    // 简单来讲,实现文件上传的时候
                    // 此时需要多设置两个属性processData与contentType
                    // 且这两个属性的值均为false即可
                    success: function (result) {
                        $("#s1").html(result);
                    }
                })
            });
        });
    </script>
</head>
<body>
<h1>文件上传</h1>
<hr>
<form id="uploadForm">
    用户名:<input type="text" name="username"><br>
    文件:<input type="file" name="file"><br>
    <input type="button" value="上传" id="upload">
    <span id="s1"></span>
</form>
</body>
</html>
